package com.muyu.danmo.controller;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCollectMaterial;
import com.muyu.danmo.service.IDanmoCollectMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 拾贝作品材质Controller
 *
 * @author yangle
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/collectMaterial")
public class DanmoCollectMaterialController extends BaseController {
    @Autowired
    private IDanmoCollectMaterialService danmoCollectMaterialService;

    /**
     * 查询拾贝作品材质列表
     */
//    @RequiresPermissions("system:collectMaterial:list")
    @GetMapping("/list")
    public Result list(DanmoCollectMaterial danmoCollectMaterial) {
        startPage();
        List<DanmoCollectMaterial> list = danmoCollectMaterialService.selectDanmoCollectMaterialList(danmoCollectMaterial);
        return getDataTable(list);
    }

    /**
     * 导出拾贝作品材质列表
     */
//    @RequiresPermissions("system:collectMaterial:export")
    @Log(title = "拾贝作品材质", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoCollectMaterial danmoCollectMaterial) {
        List<DanmoCollectMaterial> list = danmoCollectMaterialService.selectDanmoCollectMaterialList(danmoCollectMaterial);
        ExcelUtil<DanmoCollectMaterial> util = new ExcelUtil<DanmoCollectMaterial>(DanmoCollectMaterial.class);
        util.exportExcel(response, list, "拾贝作品材质数据");
    }

    /**
     * 获取拾贝作品材质详细信息
     */
//    @RequiresPermissions("system:collectMaterial:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return success(danmoCollectMaterialService.selectDanmoCollectMaterialById(id));
    }

    /**
     * 新增拾贝作品材质
     */
//    @RequiresPermissions("system:collectMaterial:add")
    @Log(title = "拾贝作品材质", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoCollectMaterial danmoCollectMaterial) {
        return toAjax(danmoCollectMaterialService.insertDanmoCollectMaterial(danmoCollectMaterial));
    }

    /**
     * 修改拾贝作品材质
     */
//    @RequiresPermissions("system:collectMaterial:edit")
    @Log(title = "拾贝作品材质", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoCollectMaterial danmoCollectMaterial) {
        return toAjax(danmoCollectMaterialService.updateDanmoCollectMaterial(danmoCollectMaterial));
    }

    /**
     * 删除拾贝作品材质
     */
//    @RequiresPermissions("system:collectMaterial:remove")
    @Log(title = "拾贝作品材质", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(danmoCollectMaterialService.deleteDanmoCollectMaterialByIds(ids));
    }

    /**
     * 拾贝收藏
     */
    @GetMapping("/isCollect/{id}")
    public Result isCollect(@PathVariable Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.warn("请登录");
        }
        return danmoCollectMaterialService.isCollect(id);
    }

    /**
     * 查看自己收藏
     */
    @PostMapping("/selectUid")
    public Result selectUid(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.warn("请登录");
        }
        List<DanmoCollectMaterial> danmoCollectMaterialList = danmoCollectMaterialService.selectUid(loginUser.getUserid());
        return success(danmoCollectMaterialList);
    }
}